package com.ffcs.crmd.tsp.admin.control;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.exception.ExceptionUtil;
import com.ffcs.crmd.platform.pub.vo.RetVo;
import com.ffcs.crmd.tsp.admin.api.dto.JobDTO;
import com.ffcs.crmd.tsp.admin.api.facade.IJobFacade;

/**
 * 
 * 功能说明:任务控制层
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年2月16日 下午4:49:00
 *
 * 版本号 | 作者 | 修改时间 | 修改内容
 *
 */
@Controller
@ResponseBody
@RequestMapping("/tsp/job")
public class JobController {
    
    private final Logger logger = LoggerFactory.getLogger(JobController.class);
    
    @Autowired
    private IJobFacade   jobFacade;
    
    /**
     * 
     * 功能说明:新增任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/add")
    public RetVo add(ModelMap map, HttpSession httpSession, @RequestBody JobDTO jobDTO) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.save(jobDTO);
        } catch (Exception e) {
            logger.error("JobController : add job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    /**
     * 
     * 功能说明:新增任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/get")
    public RetVo get(ModelMap map, HttpSession httpSession, @RequestBody JobDTO jobDTO) {
        RetVo retVo = new RetVo(true);
        try {
            JobDTO rjobDto = jobFacade.get(jobDTO);
            retVo.setObject(rjobDto);
        } catch (Exception e) {
            logger.error("JobController : add job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:删除任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/deleteList")
    public RetVo deleteList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDTO> jobDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.remove(jobDTOList);
        } catch (Exception e) {
            logger.error("JobController : delete job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    /**
     * 
     * 功能说明:删除任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/delete")
    public RetVo delete(ModelMap map, HttpSession httpSession, @RequestBody JobDTO jobDTO) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.remove(jobDTO);
        } catch (Exception e) {
            logger.error("JobController : delete job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/deleteJobdetailList")
    public RetVo deleteJobdetailList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDTO> jobDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.deleteJobdetailList(jobDTOList);
        } catch (Exception e) {
            logger.error("JobController : delete all job detail exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:更新任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/update")
    public RetVo update(ModelMap map, HttpSession httpSession, @RequestBody JobDTO jobDTO) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.update(jobDTO);
        } catch (Exception e) {
            logger.error("JobController : update job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/resume")
    public RetVo resume(ModelMap map, HttpSession httpSession, @RequestBody List<JobDTO> jobDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.resume(jobDTOList);
        } catch (Exception e) {
            logger.error("JobController : resume job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/pause")
    public RetVo pause(ModelMap map, HttpSession httpSession, @RequestBody List<JobDTO> jobDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobFacade.pause(jobDTOList);
        } catch (Exception e) {
            logger.error("JobController : pause job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:查询所有任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/query")
    public RetVo query(ModelMap map, HttpSession httpSession, @RequestBody JobDTO jobDTO) {
        RetVo retVo = new RetVo(true);
        try {
            if(StringUtils.isBlank(jobDTO.getClusterName())){
                String clusterName = httpSession.getAttribute("clusterName") == null ? null
                    : String.valueOf(httpSession.getAttribute("clusterName"));
                jobDTO.setClusterName(clusterName);
            }
            List<JobDTO> jobList = jobFacade.query(jobDTO);
            retVo.setDataList(jobList);
        } catch (Exception e) {
            logger.error("JobController : query all job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:查询所有任务
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/queryPage")
    public RetVo queryPage(ModelMap map, HttpSession httpSession, @RequestBody JobDTO jobDTO) {
        RetVo retVo = new RetVo(true);
        try {
            if(StringUtils.isBlank(jobDTO.getClusterName())){
                String clusterName = httpSession.getAttribute("clusterName") == null ? null
                    : String.valueOf(httpSession.getAttribute("clusterName"));
                jobDTO.setClusterName(clusterName);
            }
            PageInfo<JobDTO> pageInfo = jobFacade.queryPage(jobDTO);
            retVo.setPageInfo(pageInfo);
        } catch (Exception e) {
            logger.error("JobController : query all job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
}
